package com.taxsearch.controller.room;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.taxsearch.dao.QianXianMapper;
import com.taxsearch.dao.room.RoomSelectMapper;
import com.taxsearch.util.ApiJsonErr;
import com.taxsearch.util.ApiJsonRet;
import com.taxsearch.util.DbUtil;
import com.taxsearch.util.roomutil.RoomUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * @description: 管理权限
 *
 * @author: 杜一平
 *
 * @create: 2018年9月18日-上午10:50:16
 **/
@Controller
@RequestMapping("/HotelAndroid")
public class QuanXianController {
	/**
	 * 总体说明：
	 * 参数data为一个json字符串，类似为url地址后面的localhost:8080/HotelAndroid/selectRoom?data
	 * ={"status":"1"}中的data 注意调用相关方法解析
	 * 返回值为Object,其中返回ApiJsonErr默认状态为400为失败,ApiJsonRet状态为200为成功
	 */

	// 日志
	public static final Logger logger = LogManager
			.getLogger(RoomManageController.class);
	// 统一返回结果
	Object result;

	// 权限管理dao层
	@Autowired
	private QianXianMapper qianXianMapper;
	
	@Autowired
	private RoomSelectMapper roomSelectMapper;

	/**
	 * 
	 * @Title: updateQuanXian
	 * @Description: 根据猜测，更新省公司权限表
	 * @param: @param data
	 * @param: @return
	 * @return: Object
	 * @throws
	 */
	@RequestMapping("/updateQuanXian.do")
	@ResponseBody
	public Object updateQuanXian(String data) {
		try {
			// 获取相关参数
			Map<String, String> map = RoomUtil.getJsonMap(data);
			// 校验
			if (RoomUtil.validationData(map)) {
				return new ApiJsonErr("有参数为空！");
			}

			// 切换数据源为wkgl
			DbUtil.setDb("2");

			// 绑定，直接传入mybatis的xml
			int i = qianXianMapper.updateQuanXian(map);
			if (i == 1) {
				result = new ApiJsonRet("200", "绑定成功！");
			} else {
				result = new ApiJsonErr("暂未授权！");
			}
			// 数据源切回
			DbUtil.setDb("1");

		} catch (Exception e) {
			logger.error("selectQuanXian-发生错误!!!!!!!!!!!!!!!!!!!!!!!!！", e);
			result = new ApiJsonErr("服务器繁忙,请稍后再试！");
		}
		return result;
	}

	
	/**
	 * 
	 * @Title: selectQuanXian
	 * @Description: 查询电话号码的有权限的酒店
	 * @param: @param data
	 * @param: @return
	 * @return: Object
	 * @throws
	 */
	@RequestMapping("/selectQuanXian_old.do")
	@ResponseBody
	public Object selectQuanXian(String data) {
		try {
			// 切换数据源为wkgl
			DbUtil.setDb("2");
			// 获取相关参数
			Map<String, String> map = RoomUtil.getJsonMap(data);
			// 校验
			if (RoomUtil.validationData(map)) {
				return new ApiJsonErr("有参数为空！");
			}
			String phoneNumberString = map.get("phone");
			
			List<Map<String, String>> list = qianXianMapper
					.selectQuanXian(phoneNumberString);
			
			
			// 当没有找到记录时
			if (list.size() == 0) {
				return new ApiJsonErr("未找到旅馆信息，请联系管理员！");
			}
			Set<String> nameSet = new TreeSet<>();
			for (Map<String, String> mapTemp : list) {
				nameSet.add(mapTemp.get("STATIONID"));
			}

			// 获取现在的八位时间
			String nowTime = RoomUtil.getEightDateNow();
			for (Map<String, String> mapTemp : list) {
				mapTemp.put("HOTELNAMELONG", mapTemp.get("HOTELNAME"));
				// 名字超过限制...
				if (mapTemp.get("HOTELNAME").length() > 7) {
					mapTemp.put("HOTELNAME", mapTemp.get("HOTELNAME")
							.substring(0, 7) + "...");
				}

				int beginTime = Integer.parseInt(mapTemp.get("SDATE").replace(
						"-", ""));
				int endTime = Integer.parseInt(mapTemp.get("EDATE").replace(
						"-", ""));
				int nowTimeInt = Integer.parseInt(nowTime);

				if (nowTimeInt < beginTime) {
					// 授权未开始
					mapTemp.put("code", "97");
				}
				if (nowTimeInt >= beginTime && nowTimeInt <= endTime) {
					// 正常授权
					mapTemp.put("code", "0");
				}
				if (nowTimeInt >= beginTime && nowTimeInt <= endTime
						&& (endTime - nowTimeInt) <= 15) {
					// 授权不足十五天
					mapTemp.put("code", "1");
				}
				if (nowTimeInt > endTime) {
					// 授权已过期
					mapTemp.put("code", "98");
				}
			}

			// 筛选出优先状态
			Map<String, String> statusMap = new HashMap<>();
			for (int i = 0; i < list.size(); i++) {
				Map<String, String> mapTemp = list.get(i);
				if (!statusMap.containsKey(mapTemp.get("STATIONID"))) {
					statusMap
							.put(mapTemp.get("STATIONID"), mapTemp.get("code"));
				} else {
					int dangQian = Integer.parseInt(mapTemp.get("code"));
					int statusInt = Integer.parseInt(statusMap.get(mapTemp
							.get("STATIONID")));
					if (dangQian < statusInt) {
						statusMap.put(mapTemp.get("STATIONID"),
								mapTemp.get("code"));
					}
				}
			}
			
			// 只有状态不为筛选出来的最优状态就剔除
			for (int i = 0; i < list.size(); i++) {
				Map<String, String> mapTemp = list.get(i);
				if (!mapTemp.get("code").equals(
						statusMap.get(mapTemp.get("STATIONID")))) {
					list.remove(i);
					i--;
				}
			}
			System.out.println("返回成功");
			result = new ApiJsonRet("200", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("selectQuanXian-发生错误!!!!!!!!!!!!!!!!!!!!!!!!！", e);
			result = new ApiJsonErr("服务器繁忙,请稍后再试！");
		}
		return result;
	}

}
